﻿/*
This file is part of Ingo Karstein's Web based PowerShell Console for SharePoint project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spwebpsconsole.codeplex.com/
  
The project is based upon my project "Web based PowerShell Console":
  http://webpsconsole.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Newtonsoft.Json;

namespace ik.SharePoint2010.SPWebPSConsole
{
    class OutputObject
    {
        protected string OutputObjectType = "base";

        public virtual string ToJson()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using( JsonWriter jsonWriter = new JsonTextWriter(sw) )
            {
                jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("Type");
                jsonWriter.WriteValue(OutputObjectType);
                jsonWriter.WriteEndObject();
            }

            sw.Flush();
            sw.Close();
            return sb.ToString();
        }

    }

    class OutputObjectMessage : OutputObject
    {

        public string Message;
        public Color foreColor;
        public Color backColor;
        public bool newLine;
        public bool isRunning;
        public string CorrelationId;

        public OutputObjectMessage()
        {
            OutputObjectType = "message";
            foreColor = Color.White;
            backColor = Color.Black;
            newLine = true;
            Message = "";
        }

        public override string ToJson()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using( JsonWriter jsonWriter = new JsonTextWriter(sw) )
            {
                jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("Type");
                jsonWriter.WriteValue(OutputObjectType);
                jsonWriter.WritePropertyName("Message");
                jsonWriter.WriteValue(Message);
                jsonWriter.WritePropertyName("ForeColor");
                jsonWriter.WriteValue(System.Drawing.ColorTranslator.ToHtml(foreColor));
                jsonWriter.WritePropertyName("BackColor");
                jsonWriter.WriteValue(System.Drawing.ColorTranslator.ToHtml(backColor));
                jsonWriter.WritePropertyName("newLine");
                jsonWriter.WriteValue(newLine);
                jsonWriter.WritePropertyName("CorrelationId");
                jsonWriter.WriteValue(CorrelationId);
                jsonWriter.WritePropertyName("IsRunning");
                jsonWriter.WriteValue(isRunning);
                jsonWriter.WriteEndObject();
            }

            sw.Flush();
            sw.Close();
            return sb.ToString();
        }
    }

    class OutputObjectTraceMessage : OutputObjectMessage
    {
        public OutputObjectTraceMessage()
            : base()
        {
            OutputObjectType = "tracemessage";
        }
    }

    class OutputObjectClearHost : OutputObjectMessage
    {
        public OutputObjectClearHost()
            : base()
        {
            OutputObjectType = "clearhost";
        }
    }

    class OutputObjectPipeFinished : OutputObjectMessage
    {
        public OutputObjectPipeFinished()
            : base()
        {
            OutputObjectType = "pipefinished";
        }
    }

    class OutputObjectEndSession : OutputObject
    {
        public OutputObjectEndSession()
            : base()
        {
            OutputObjectType = "endsession";
        }
    }

    class OutputObjectPipeState : OutputObject
    {
        public int PipeState;
        public string CorrelationId;
        public string Reason;

        public OutputObjectPipeState()
        {
            OutputObjectType = "pipestate";
            PipeState = -1;
        }

        public override string ToJson()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using( JsonWriter jsonWriter = new JsonTextWriter(sw) )
            {
                jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("Type");
                jsonWriter.WriteValue(OutputObjectType);
                jsonWriter.WritePropertyName("PipeState");
                jsonWriter.WriteValue(PipeState);
                jsonWriter.WritePropertyName("CorrelationId");
                jsonWriter.WriteValue(CorrelationId);
                jsonWriter.WritePropertyName("Reason");
                jsonWriter.WriteValue(Reason);
                jsonWriter.WriteEndObject();
            }

            sw.Flush();
            sw.Close();
            return sb.ToString();
        }
    }

    class OutputObjectUserRequest : OutputObject
    {
        public string RequestCorrelationId;
        public string Description = "";
        public string Data = "";

        public OutputObjectUserRequest()
        {
            OutputObjectType = "userrequest";
        }

        public override string ToJson()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using( JsonWriter jsonWriter = new JsonTextWriter(sw) )
            {
                jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("Type");
                jsonWriter.WriteValue(OutputObjectType);
                jsonWriter.WritePropertyName("RequestCorrelationId");
                jsonWriter.WriteValue(RequestCorrelationId);
                jsonWriter.WritePropertyName("Description");
                jsonWriter.WriteValue(Description);
                jsonWriter.WritePropertyName("Data");
                jsonWriter.WriteValue(Data);
                jsonWriter.WriteEndObject();
            }

            sw.Flush();
            sw.Close();
            return sb.ToString();
        }
    }
}
